﻿using Microsoft.Extensions.Logging;
using ST.Common.Util;
using ST.WX.Common;
using ST.WX.Common.Model;
using ST.WX.MP.Menu.Model;
using ST.WX.MP.Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace ST.WX.MP.Menu
{
	public class WXMenuApi
	{
		private readonly ILogger logger;
		private readonly WXBaseApi baseApi;

		public WXMenuApi(ILogger<WXMenuApi> _logger, WXBaseApi _baseApi)
		{
			logger = _logger;
			baseApi = _baseApi;
		}

		/// <summary>
		/// 自定义菜单查询接口
		/// </summary>
		/// <param name="appID"></param>
		/// <returns></returns>
		public WXMenu Get(string appID, string appSecret)
		{
			try
			{
				string respText = HttpHelper.Get("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + baseApi.GetAccessToken(appID, appSecret));
				if (string.IsNullOrEmpty(respText))
				{
					throw new WXException("自定义菜单查询返回为空");
				}
				WXMenuResult ret = Newtonsoft.Json.JsonConvert.DeserializeObject<WXMenuResult>(respText);
				if (ret.errcode != 0)
				{
					throw new WXException(ret.errmsg);
				}
				else
				{
					return ret.menu;
				}
			}
			catch (WXException ex)
			{
				logger.LogError(ex, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "自定义菜单查询失败");
			}
			return null;
		}

		/// <summary>
		/// 测试个性化菜单匹配结果
		/// </summary>
		/// <param name="appID"></param>
		/// <param name="userid">粉丝的OpenID或微信号</param>
		/// <returns></returns>
		public WXConditionalMenuMatchResult GetConditional(string appID, string appSecret, string userid)
		{
			try
			{
				if (string.IsNullOrEmpty(userid))
				{
					throw new WXException("userid不能为空");
				}
				string respText = HttpHelper.Post("https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=" + baseApi.GetAccessToken(appID, appSecret), "{\"user_id\":\"" + userid + "\"}");
				if (string.IsNullOrEmpty(respText))
				{
					throw new WXException("测试个性化菜单匹配结果返回为空");
				}
				WXConditionalMenuMatchResult ret = Newtonsoft.Json.JsonConvert.DeserializeObject<WXConditionalMenuMatchResult>(respText);
				if (ret.errcode == 0)
				{
					return ret;
				}
				else
				{
					throw new WXException(ret.errmsg);
				}
			}
			catch (WXException ex)
			{
				logger.LogError(ex, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "测试个性化菜单匹配结果失败");
			}
			return null;
		}

		/// <summary>
		/// 获取当前使用的自定义菜单的配置
		/// </summary>
		/// <param name="appID"></param>
		public WXCurrentMenuResult GetCurrent(string appID, string appSecret)
		{
			try
			{
				string respText = HttpHelper.Get("https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + baseApi.GetAccessToken(appID, appSecret));
				if (string.IsNullOrEmpty(respText))
				{
					throw new WXException("获取当前使用的自定义菜单返回为空");
				}
				WXCurrentMenuResult ret = Newtonsoft.Json.JsonConvert.DeserializeObject<WXCurrentMenuResult>(respText);
				if (ret.errcode == 0)
				{
					return ret;
				}
				else
				{
					throw new WXException(ret.errmsg);
				}
			}
			catch (WXException ex)
			{
				logger.LogError(ex, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "获取当前使用的自定义菜单失败");
			}
			return null;
		}

		/// <summary>
		/// 自定义菜单创建接口
		/// </summary>
		/// <param name="appID"></param>
		/// <param name="menu"></param>
		/// <returns></returns>
		public bool Create(string appID, string appSecret, WXMenu menu)
		{
			try
			{
				if (menu == null)
				{
					throw new WXException("menu不能为空");
				}
				string menuJson = Newtonsoft.Json.JsonConvert.SerializeObject(menu);
				string respText = HttpHelper.Post("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + baseApi.GetAccessToken(appID, appSecret),
					menuJson);
				if (string.IsNullOrEmpty(respText))
				{
					throw new WXException("创建自定义菜单返回为空");
				}
				WXResult ret = Newtonsoft.Json.JsonConvert.DeserializeObject<WXResult>(respText);
				if (ret.errcode == 0)
				{
					return true;
				}
				else
				{
					throw new WXException(ret.errmsg);
				}
			}
			catch (WXException ex)
			{
				logger.LogError(ex, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "自定义菜单创建失败");
			}
			return false;
		}

		/// <summary>
		/// 创建个性化菜单
		/// </summary>
		/// <param name="appID"></param>
		/// <param name="menu"></param>
		/// <returns>返回个性化菜单id</returns>
		public string CreateConditional(string appID, string appSecret, WXConditionalMenu menu)
		{
			try
			{
				if (menu == null)
				{
					throw new WXException("menu不能为空");
				}
				string menuJson = Newtonsoft.Json.JsonConvert.SerializeObject(menu);
				string respText = HttpHelper.Post("https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=" + baseApi.GetAccessToken(appID, appSecret),
					menuJson);
				if (string.IsNullOrEmpty(respText))
				{
					throw new WXException("创建个性化菜单返回为空");
				}
				WXConditionalMenuResult ret = Newtonsoft.Json.JsonConvert.DeserializeObject<WXConditionalMenuResult>(respText);
				if (ret.errcode == 0)
				{
					return ret.menuid;
				}
				else
				{
					throw new WXException(ret.errmsg);
				}
			}
			catch (WXException ex)
			{
				logger.LogError(ex, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "个性化菜单创建失败");
			}
			return string.Empty;
		}

		/// <summary>
		/// 自定义菜单删除接口
		/// </summary>
		/// <param name="appID"></param>
		/// <returns></returns>
		public bool Delete(string appID, string appSecret)
		{
			try
			{
				string respText = HttpHelper.Get("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + baseApi.GetAccessToken(appID, appSecret));
				if (string.IsNullOrEmpty(respText))
				{
					throw new Exception("自定义菜单删除返回为空");
				}
				WXResult ret = Newtonsoft.Json.JsonConvert.DeserializeObject<WXResult>(respText);
				if (ret.errcode == 0)
				{
					return true;
				}
				else
				{
					throw new WXException(ret.errmsg);
				}
			}
			catch (WXException ex)
			{
				logger.LogError(ex, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "自定义菜单删除失败");
			}
			return false;
		}

		/// <summary>
		/// 删除个性化菜单
		/// </summary>
		/// <param name="appID"></param>
		/// <param name="menuid"></param>
		/// <returns></returns>
		public bool DeleteConditional(string appID, string appSecret, string menuid)
		{
			try
			{
				if (string.IsNullOrEmpty(menuid))
				{
					throw new WXException("menuid不能为空");
				}
				string respText = HttpHelper.Post("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + baseApi.GetAccessToken(appID, appSecret), "{\"menuid\":\"" + menuid + "\"}");
				if (string.IsNullOrEmpty(respText))
				{
					throw new Exception("个性化菜单删除返回为空");
				}
				WXResult ret = Newtonsoft.Json.JsonConvert.DeserializeObject<WXResult>(respText);
				if (ret.errcode == 0)
				{
					return true;
				}
				else
				{
					throw new WXException(ret.errmsg);
				}
			}
			catch (WXException ex)
			{
				logger.LogError(ex, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "个性化菜单删除失败");
			}
			return false;
		}
	}
}
